<template>
  <uni-popup ref="search" type="top">
    <view style="background-color: #ffffff; padding: 20rpx">
      <form @submit="formSubmit" @reset="formReset">
        <view class="uni-form-item">
          <view class="title">入库单号</view>
          <view class="uni-input-view">
            <input
              class="uni-input"
              name="billNo"
              v-model="billNo"
              :focus="focus[0]"
              @confirm="handleFocus(1)"
              placeholder="请扫描入库单号"
            />
            <image
              src="/static/scan.png"
              class="scan"
              @click="handleScan('billNo', 1)"
            />
          </view>
        </view>
        <view class="uni-form-item">
          <view class="title">外部单号</view>
          <view class="uni-input-view">
            <input
              class="uni-input"
              name="outOrderNo"
              v-model="outOrderNo"
              :focus="focus[1]"
              @confirm="handleFocus"
              placeholder="请扫描外部单号"
            />
            <image
              src="/static/scan.png"
              class="scan"
              @click="handleScan('outOrderNo')"
            />
          </view>
        </view>
        <view class="uni-form-item">
          <view class="title">货主代码</view>
          <picker
            @change="handleOwnerChange"
            :value="ownerIndex"
            :range="ownerList"
            range-key="label"
            style="flex: 1"
          >
            <view class="uni-input border">{{
              ownerList[ownerIndex].label
            }}</view>
          </picker>
        </view>
        <view class="uni-form-item">
          <view class="title">供应商名称</view>
          <input
            class="uni-input border"
            name="supplierName"
            v-model="supplierName"
            placeholder="请输入供应商名称"
          />
        </view>
        <view class="uni-form-item">
          <view class="title">{{
            current === 0 ? '预检完成' : '质检完成'
          }}</view>
          <switch :checked="inBillStateChecked" @change="handleStateChange" />
        </view>
        <view class="uni-btn-v">
          <button type="primary" form-type="submit">查询</button>
          <button type="default" form-type="reset">重置</button>
        </view>
      </form>
    </view>
  </uni-popup>
</template>

<script>
import uniPopup from '@/components/uni-popup/uni-popup.vue';

export default {
  components: {
    uniPopup
  },
  props: {
    current: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      focus: [false, false],
      billNo: null,
      outOrderNo: null,
      supplierName: null,
      ownerIndex: 0,
      ownerList: [
        {
          value: '',
          label: '请选择货主代码'
        }
      ],
      inBillStateChecked: false,
      inBillState: null
    };
  },
  methods: {
    open(params) {
      this.$set(this.focus, 0, false);
      setTimeout(() => this.$set(this.focus, 0, true), 500);
      this.billNo = params.billNo || null;
      this.outOrderNo = params.outOrderNo || null;
      this.supplierName = params.supplierName || null;
      this.$api.inBill
        .ownerList({
          limit: 1000,
          page: 1
        })
        .then(res => {
          this.ownerList = res.data.map(v => {
            return {
              value: v.code,
              label: `${v.code}-${v.shortName}`
            };
          });
          this.ownerList.splice(0, 0, {
            value: '',
            label: '请选择货主代码'
          });
          this.ownerIndex = params.ownerId
            ? this.ownerList.findIndex(v => v.value === params.ownerId)
            : 0;
        });
      this.inBillState = params.inBillState || null;
      if (this.inBillState) {
        this.inBillStateChecked = true;
      } else {
        this.inBillStateChecked = false;
      }
      this.$refs.search.open();
    },
    handleScan(key, focusIndex) {
      this.$scan(res => {
        this[key] = res;
        this.handleFocus(focusIndex);
      });
    },
    handleFocus(index) {
      this.focus.map((v, i) => this.$set(this.focus, i, false));
      index && setTimeout(() => this.$set(this.focus, index, true), 100);
    },
    handleOwnerChange(e) {
      this.ownerIndex = e.target.value;
    },
    handleStateChange(e) {
      this.inBillStateChecked = e.detail.value;
    },
    formSubmit(e) {
      const params = Object.assign({}, e.detail.value, {
        ownerId: this.ownerIndex ? this.ownerList[this.ownerIndex].value : null,
        inBillState: this.inBillStateChecked
          ? this.current === 0
            ? '300'
            : '500'
          : null
      });
      this.$refs.search.close();
      this.$emit('search', params);
    },
    formReset() {
      this.ownerIndex = 0;
      this.inBillStateChecked = false;
      this.inBillState = null;
      this.$refs.search.close();
      this.$emit('search');
    }
  }
};
</script>

<style lang="scss" scoped>
.uni-btn-v {
  display: flex;
  justify-content: space-between;

  button {
    width: 45%;
    font-size: 28rpx;
  }
}
</style>
